package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.app.service.DeScoresService;
import com.campus.app.service.StudentsService;
import com.campus.commons.entity.Event;
import com.campus.app.mapper.EventMapper;
import com.campus.app.service.EventsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 * 任务表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
public class EventServiceImpl extends ServiceImpl<EventMapper, Event> implements EventsService {
@Autowired
private EventMapper eventMapper;
@Autowired
private StudentsService studentsService;

@Autowired
private DeScoresService deScoresService;
    @Override
    public Result<List<Event>> getAllEvents(MyPage myPage, Integer deId) {
        LambdaQueryWrapper<Event> queryWrapper = new QueryWrapper<Event>().lambda();
        queryWrapper.eq(Event::getDeId, deId);
        return Result.result(eventMapper.selectPage(new Page<>(myPage.getPage(),myPage.getLimit()),queryWrapper));
    }

    @Override
    public Result<List<Event>> getEvents(MyPage<Event> myPage) {
//        多条件查询； deId  state  studentId
        LambdaQueryWrapper<Event> queryWrapper = new QueryWrapper<Event>().lambda();
        Event data=myPage.getData();
        if (data==null) {
            return Result.error(Error.PARAMS_E3R,"缺少查询条件数据");
        }
        queryWrapper.eq(data.getDeId()!=null,Event::getDeId, data.getDeId())
        .eq(data.getState()!=null,Event::getState,data.getState())
                .likeRight(data.getEventName()!=null,Event::getEventName,data.getEventName())
        .eq(data.getStudentId() != null , Event::getStudentId,data.getStudentId());
     return Result.result(eventMapper.selectPage(new Page<>(myPage.getPage(),myPage.getLimit()),queryWrapper));
    }

    @Override
    public Result addEvent(Event event) {
        return Result.result( eventMapper.insert(event));
    }

    @Override
    public Result delEvent(Integer id) {
//        撤销任务； 只有领取的可以撤销； 只有当给记录抽出来的时候才能在进行别的删除；
        Event byId = getById(id);
        if (byId==null) {
            return Result.error(Error.PARAMS_E3R, "没有找到该记录信息");
        }
        if (byId.getState()!=0) {
            return Result.error(Error.Operation_E1R, "该任务已被领取，不允许撤销该任务");
        }
        return  Result.result( eventMapper.deleteById(id));
    }

    @Override
    public Result getTheTask(Integer id, Integer studentId) {
        Event event = eventMapper.selectById(id);
        if (event==null) {
            return Result.error(Error.PARAMS_E3R, "找不到该任务，参数eventId错误");
        }
        if (event.getState() != 0) {
            return Result.error(Error.Operation_E1R, "该任务已经被领取过了，不能再次领取");
        }
        boolean isDepartment = studentsService.isDepartment(studentId);
        if (!isDepartment) {
            return Result.error(Error.PARAMS_E3R, "当前使用的账号不是学社成员无法领取任务，");
        }
        event.setState(1);
        event.setStudentId(studentId);
        return Result.result(eventMapper.updateById(event));
    }

    @Override
    public Result updateEvent(Event event) {
        Event byId = getById(event.getId());
        if (byId.getState()!=0) {
            return Result.error(Error.Operation_E1R, "该任务内容不允许被编辑");
        }
        return Result.result(updateById(event));
    }

    @Override
    public Result updateState(Integer id, Integer state) {
        Event event = getById(id);
        Integer stat = event.getState();
        if (event==null) {
            return Result.error(Error.PARAMS_E3R, "找不到该任务，参数eventId错误");
        }
        if (stat !=1) {
            String msg = stat == 0 ? "该任务还没有被领取不能处理" : "该任务已经被处理过了，不能再次处理";
            return Result.error(Error.Operation_E1R, msg);
        }
        event.setState(state);
        boolean res = updateById(event);
        if (state==3&&res) {
            //更新成功还得；将积分添加到对应的学生账号
            try {
               boolean b= deScoresService.saveOrUpdateScore(event.getStudentId(), event.getEventScore());
                if (!b) {
                    return Result.error(Error.System_E1R, "发生异常：本次积分没有加载上");
                }
            } catch (RuntimeException e) {
                return Result.error(Error.PARAMS_E3R, e.getStackTrace());
            }
        }
        return Result.result(res);
    }
}
